package ar.com.ford.it.fullEconomics.view.displaytag;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.jsp.PageContext;
import org.displaytag.decorator.DisplaytagColumnDecorator;
import org.displaytag.exception.DecoratorException;
import org.displaytag.properties.MediaTypeEnum;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;
import com.ibm.icu.text.SimpleDateFormat;

public class CostStructureItemColumnDecorator implements DisplaytagColumnDecorator {
    
    public Object decorate(Object column, PageContext pageContext, MediaTypeEnum media) throws DecoratorException {
        String columnValue = (String) column;
        String[] valueSplitted = columnValue.split("<br />");
        String ret = null;
        String aux = null;

        if(valueSplitted.length <= 1) {
            ret = columnValue;
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            aux = valueSplitted[1];
            aux = aux.trim();
            try {
                Date date = format.parse(aux);
                calendar.setTime(date);
                ret = valueSplitted[0] + "<br />" + FullEconomicsDateUtil.format(calendar);
            } catch (ParseException e) {
                ret = null;                
            }
        }

        return ret;
    }
}
